//
//  NoticeBilboardViewController.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/7/4.
//

import UIKit
import SnapKit
import JXSegmentedView
import RxSwift
import RxCocoa

/// 公告通知
class NoticeBilboardViewController: UIViewController {

  let titles: [NoticeListType] = [.myNotice, .published]
  let segmentedView = JXSegmentedView()
  let segmentedDataSource = JXSegmentedTitleDataSource()
  let disposeBag = DisposeBag()

  lazy var listContainerView = JXSegmentedListContainerView(dataSource: self)

  let sheetHandler = PTSheetHandle()

  var onlyNotice: Bool = false

  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    opacityNavigationBar(color: .designKit.background)

    title = "公告通知"
    view.backgroundColor = .designKit.background

    let searchView = R.nib.searchContentView(owner: self)!
    searchView.textField.placeholder = "搜索公告标题"
    view.addSubview(searchView)

    segmentedView.delegate = self
    view.addSubview(segmentedView)

    segmentedDataSource.titles = titles.map { $0.rawValue }
    segmentedDataSource.isTitleColorGradientEnabled = true
    segmentedView.dataSource = self.segmentedDataSource
    segmentedView.backgroundColor = .designKit.background
    segmentedDataSource.titleSelectedColor = .designKit.accent

    let indicator = JXSegmentedIndicatorLineView()
    indicator.indicatorWidth = 42
    indicator.indicatorColor = .designKit.accent
    segmentedView.indicators = [indicator]

    segmentedView.listContainer = listContainerView
    view.addSubview(listContainerView)

    searchView.snp.makeConstraints { make in
      make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
      make.left.right.equalToSuperview()
      make.height.equalTo(56)
    }

    if onlyNotice == false {
      segmentedView.snp.makeConstraints { make in
        make.top.equalTo(searchView.snp.bottom)
        make.left.right.equalToSuperview()
        make.height.equalTo(36)
      }
    }

    listContainerView.snp.makeConstraints { make in
      if onlyNotice == false {
        make.top.equalTo(segmentedView.snp.bottom)
      } else {
        make.top.equalTo(searchView.snp.bottom)
      }
      make.left.right.bottom.equalToSuperview()
    }

    let button = UIButton(type: .custom)
    button.setBackgroundImage(R.image.icon_publish()!, for: .normal)
    view.addSubview(button)
    button.snp.makeConstraints { make in
      make.right.equalToSuperview().offset(15)
      make.bottom.equalToSuperview().offset(-88)
      make.size.equalTo(CGSize(width: 48, height: 48))
    }

    button.rx.tap.subscribe(onNext: {
      self.jumpToPublish()
    })
    .disposed(by: disposeBag)
  }

  func jumpToPublish() {
    let vc = NoticePublishViewController()
    vc.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(vc, animated: true)
  }

  /*
   // MARK: - Navigation

   // In a storyboard-based application, you will often want to do a little preparation before navigation
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   // Get the new view controller using segue.destination.
   // Pass the selected object to the new view controller.
   }
   */

}

extension NoticeBilboardViewController: JXSegmentedViewDelegate {
}

extension NoticeBilboardViewController: JXSegmentedListContainerViewDataSource {

  func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
    return segmentedDataSource.titles.count
  }

  func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {

    let vc = NoticeListViewController()
    vc.type = titles[index]
    return vc
  }
}
